// const host = 'http://fangfu.cn1.utools.club/fangfu-api/' //本地
const host = 'https://gateway.qswh.net.cn/apip/' //线上地址
// const host = 'http://192.168.2.82:9100/apip/' //本地
// const host = 'http://fangfu.cn.utools.club/fangfu-api/' //本地



let Promises = Promise

Promises.prototype.finally = function(callback) {
	let P = this.constructor
	return this.then(
		value => P.resolve(callback()).then(() => value),
		reason => P.resolve(callback()).then(() => {
			throw reason
		}))
}

class Https {
	host() {
		return host
	}
	imgUrl(){
		return imgUrl
	}
	request(url, method, data, type) {
		const token = uni.getStorageSync('token')
    // console.log('参数',data)
		// uni.showLoading({
		// 	title: '加载中'
		// });
		return new Promises((resolve, reject) => {
      // console.log(host + url)
      // console.log('data',data)
			uni.request({
				url: host + url, // 接口地址
				method: method, // 接口类型
				// dataType:JSON,
				data: {
					...data
				},
				header: { 
					'Authorization':'Bearer '+token,
					// 'content-type':'application/x-www-form-urlencoded'
          'content-type':data.type==1?'application/json':'application/x-www-form-urlencoded'
					// 'content-type': 'application/json' //'application/x-www-form-urlencoded'   // 默认值
				},
				// beforeSend: function(request) {
				// 	request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
				// },
				success: function(res) {
					uni.hideLoading() // 关闭加载中
					// console.log(res)
          resolve(res.data)
					
				},
				fail: function(error) {
					uni.hideLoading() // 关闭加载中
					reject(error)
				}
			})
		})
		uni.hideLoading() // 关闭加载中
	}

	get(url, data) {
		return this.request(url, 'GET', data, 0)
	}

	post(url, data) {
		return this.request(url, 'POST', data, 0)
	}
  put(url, data) {
		return this.request(url, 'put', data, 0)
	}
	postNoFormat(url, data) {
		return this.request(url, 'POST', data, 1)
	}
	
	// 获取时间 年月日时分秒
	timeFormat(timestamp,format){
	  //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	    var time = new Date(timestamp);
	    var year = time.getFullYear();
	    var month = time.getMonth()+1;
		month = month > 9? month:'0'+month
	    var date = time.getDate();
		date = date > 9? date:'0'+date
	    var hours = time.getHours();
		hours = hours > 9? hours:'0'+hours
	    var minutes = time.getMinutes();
		minutes = minutes > 9? minutes:'0'+minutes
	    var seconds = time.getSeconds();
		seconds = seconds > 9? seconds:'0'+seconds
		if (format == 'yyyy-MM-dd hh:mm:ss') {
			return year+'-'+ month +'-'+date+' '+hours+':'+minutes+':'+seconds;
		}else if (format == 'yyyy-MM-dd hh:mm') {
			return year+'-'+ month +'-'+date+' '+hours+':'+minutes;
		}else if (format == 'y-M-d h:m') {
			return year+'年'+ month +'月'+date+'日 '+hours+':'+minutes;
		}else if (format == 'yyyy-MM-dd') {
			return  year+'-'+ month +'-'+date;
		}
	}
	
	// 上传图片
	upFile(tempFilePath,data,url) {
    uni.showLoading()
    const token = uni.getStorageSync('token')
		return new Promises((resolve, reject) => {
      // console.log(tempFilePath);
      // console.log(host + '/envirnment-basic/app/task/execute');
			uni.uploadFile({
				url: host + (url?url:'/envirnment-basic/app/task/execute'),
        files:tempFilePath,
				// filePath: tempFilePaths[0],
				 // name: 'file',
				formData: {
          ...data
					// token: uni.getStorageSync('qiniuToken'),
					// key: timestamp + Math.floor(Math.random() * 10000) + '.png'
				},
         header: {
           	'Authorization':'Bearer '+token,
                  contentType: "application/x-www-form-urlencoded",
        },
				success(res) {
          uni.hideLoading()
					console.log('res', res)
					let imgKey = JSON.parse(res.data)
					resolve(imgKey)
					// console.log(imgKey)
					// console.log(imgKey, 'apiiiiiiiiiiii')
					// if (imgKey.code == 200) {
					// 	resolve(imgKey)
					// } else {
					// 	resolve(imgKey)
					// 	uni.showToast({
					// 		title: '上传失败',
					// 		icon: 'none',
					// 		duration: 2000
					// 	})
					// }
				},
				fail: function(error) {
           uni.hideLoading()
          console.log(error);
					resolve(error)
					uni.showToast({
						title: '上传失败',
						icon: 'none',
						duration: 2000
					})
				}
			})
		})
	}
	// uniApi
	uniApi(params, data) {
		return new Promises((resolve, reject) => {
			params.events({
				...data,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}
}

const https = new Https()

export default https
